package com.xudow.app.newui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.course.CourseSetUpWithOtherInfoMessage;
import com.activeshare.edu.ucenter.common.messages.user.InspectionTeamMessage;
import com.activeshare.edu.ucenter.models.course.CourseSetUpItem;
import com.activeshare.edu.ucenter.models.user.InspectionTeam;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.util.FileUtils;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取课程列表Task
 *
 * @author lukecheng
 */
public class InspectionTeamTask extends AsyncTask<Void, Void, Void> {
    private static final String TAG = makeLogTag(InspectionTeamTask.class);

    private String url = Config.BASE_URL + Config.INSPECTION_TEAM_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public InspectionTeamTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }


    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    private void sendSuccessMessage(List<InspectionTeam> courses) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();

        if (courses != null) {
            message.obj=courses;
        }

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(Void... params) {
        XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        //restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        LinkedMultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();


        try {

            InspectionTeamMessage result=restTemplate.postForObject(url, valueMap, InspectionTeamMessage.class);
            //String result=restTemplate.postForObject(url,valueMap,String.class);
            LOGE("zxj", "url = " + url);

            if (result != null) {
                //FileUtils.writeFile(result, new File(context.getCacheDir().getAbsolutePath(), "courseSetUp.txt"));
                if (result.getResult().equals("0")) {
                    sendSuccessMessage(result.getTeams());
                } else if (result.getResult().equals("1")) {
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage() );
            sendErrorMessage(e.getMessage());
        }

        return null;
    }


}
